from appium import webdriver
from selenium.webdriver.common.by import By
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import base64

abc = {
    "platformName": "Android",
    "platformVersion": "7.1.2",
    "deviceName": "emulator-5554",
    "appPackage": "com.android.settings",
    "appActivity": ".Settings"
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', abc)
# 隐式等待
driver.implicitly_wait(10)
# id定位
# ele = driver.find_element(By.ID, "com.android.settings:id/search")
# ele.click()
# XPATH定位
# ele = driver.find_element(By.XPATH, "//*[@text='使用指纹']")
# ele.click()
# # class定位
# ele = driver.find_element(By.CLASS_NAME, "android.widget.EditText")
# ele.send_keys("坚果")
# # 定位一组元素
# eles = driver.find_elements(By.CLASS_NAME, "android.widget.TextView")
# for i in eles:
#     print(i)
# ele = WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, "android.widget.EditText")))
# ele.send_keys("坚果")
# print(f"输入框内的文本信息是:{ele.text}")
# print(f"输入框内的name是:{ele.get_attribute('name')}")
# print(f"输入框内的text是:{ele.get_attribute('text')}")
# print(f"输入框内的className是:{ele.get_attribute('className')}")
# print(f"输入框内的resourceId是:{ele.get_attribute('resourceId')}")
# print(f"元素在屏幕上的坐标是：{ele.location}")
# time.sleep(5)
# # 清空输入框内容
# ele.clear()
# print(f"当前包名是：{driver.current_package}")
# print(f"当前启动名是：{driver.current_activity}")
# for i in range(5):
#     driver.swipe(start_x=500, start_y=1800, end_x=500, end_y=650, duration=1)
#     time.sleep(1)
# driver.scroll(origin_el=driver.find_element(By.XPATH, "//*[@text='显示']"), destination_el=driver.find_element(By.XPATH, "//*[@text='使用指纹']"))
# time.sleep(5)
# driver.start_activity("com.android.launcher3", ".Launcher")
# driver.drag_and_drop(driver.find_element(By.XPATH, "//*[@text='百度贴吧']"), driver.find_element(By.XPATH, "//*[@text='百度']"))
# # 当前应用放置后台运行
# driver.background_app(5)
driver.find_element(By.XPATH, "//*[@text='更多']").click()
time.sleep(2)
driver.back()
# driver.set_network_connection(1)
# 截图并保存为文件
# driver.get_screenshot_as_file('./img.png')
# # 获取界面宽高
# print(driver.get_window_size())
driver.start_activity("com.android.gallery3d", ".app.GalleryActivity")
# driver.swipe(start_x=530, start_y=870, end_x=530, end_y=870, duration=0)
TouchAction(driver).press(x=500, y=870).wait(500).move_to(x=500, y=870).release().perform()
time.sleep(1)

time.sleep(5)
driver.quit()